import * as util from '../../utils/util';
import * as config from '../../config'
import * as API from '../../api/store';

let app = getApp();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    shopName: '',
    shopLogo:'',
    photo: '',
    authentication: false,
    hasLogin: false
    // extra_data: {
    //   token: '',
    //   key: ''
    // }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    // this.loginAction()
    // var value = wx.getStorageSync('token')
    // if(token){
    //   this.setData({hasLogin: true})
    // }
  },
  gotoLogin: function () {
    util.preventMoreClick(this, () => {
      // app.globalData.redirect = {
      //   type: 'switchTab',
      //   url: '/pages/me/me'
      // };
      wx.navigateTo({
        url: '../login/login?loginReturn=me',
      })
    });
  },

  gotoSuggestion: function () {
    util.preventMoreClick(this, () => {
      wx.navigateTo({
        url: './feedback/feedback?phone=' + this.data.phone,
      })
    })
  },

  gotoAbout: function () {
    util.preventMoreClick(this, () => {
      wx.navigateTo({
        url: './about/about?phone=' + this.data.phone,
      })
    })
  },

  gotoStoreSetting: function () {
    // if (this.data.hasLogin) {
      util.preventMoreClick(this, () => {
        wx.navigateTo({
          url: './storeSetting/storeSetting',
        })
      })
    // } else {
    //   this.gotoLogin();
    // }
  },

  gotoSetting: function () {
    if (this.data.hasLogin) {
      util.preventMoreClick(this, () => {
        wx.navigateTo({
          url: '../me/account/accountSetting',
        })
      })
    } else {
      this.gotoLogin();
    }
  },

  merchantsAuthentication: function () {
    util.preventMoreClick(this, () => {
      wx.navigateTo({
        url: './merchantsAuthentication/merchantsAuthentication',
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      phone: app.globalData.userInfo.phone,
      // shopName: app.globalData.userInfo.shopName,
      // shopLogo: app.globalData.userInfo.shopLogo,
      hasLogin: app.globalData.userInfo.hasLogin,
      authentication: app.globalData.userInfo.authentication
    });
    this.getList()
  },
  getList(){
    wx.showLoading({
      title: '正在加载数据'
    })
    API.getStore().then(res => {
      wx.hideLoading();
      this.setData({
        shopName: res.data && res.data.shopName,
        shopLogo: res.data && res.data.shopLogo
      });
    }, err => {
      wx.hideLoading();
    })
  },
  setKey: function () {

    // setTimeout(() => {
    //   this.setData({
    //     'extra_data.key': new Date().getTime()
    //   });
    // }, 100)
  },
  // resetData() {
  //   this.setData({
  //     phone: '',
  //     photo: '',
  //     authentication: false,
  //     hasLogin: false
  //   });
  // },
  onShareAppMessage: function (ops) {
    return {
      title: '邀请您使用速来饭铺小程序!',
      path: '/pages/home/home',
      imageUrl: config.imageUrl+'me/share1.png',
      success: function () {
        util.toast('分享成功');
      },
      fail: function () {
        if (err === '999') {
          util.toast('身份验证已失效!');
        } else {
          util.toast('分享失败');
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})